<?php

function paging($url, $rows_per_page, $pages_per_page, $num_rows, $page, $extra)
{	
	$numPages = ceil($num_rows/$rows_per_page);
	$offset = ($page-1)*$rows_per_page;
	$haft_limit = floor($pages_per_page/2);
	
	$list_page = "";
	if ($numPages > 1) 
	{ 
		$lowLimit = 0;
		$hiLimit = 0;
		
		if($page - $haft_limit < 1)
		{
			$lowLimit = 1;
			$highLimit = $numPages < $pages_per_page ? $numPages : ($lowLimit + ($pages_per_page-1));
		}
		else
		{
			$highLimit = $page+$haft_limit < $numPages ? $page+$haft_limit : $numPages;
			$lowLimit = $highLimit - ($pages_per_page-1);
			if($lowLimit < 1)
				$lowLimit = 1;
		}			
		
		
		if($page > 1)
		{
			$list_page .= "<td><a href='#'><b style='color:#0000FF' onClick='GoTo(1)'>Đầu</b></a>&nbsp;&nbsp;</td> "; 
			$prev = $page - 1;
			$list_page .= "<td><a href='#'><b style='color:#0000FF' onClick='GoTo($prev)'>Trước</b><a>&nbsp;&nbsp;&nbsp;&nbsp;</td> "; 
		}
		else
		{
			$list_page .= "<td><b style='color:#0000FF'>Đầu</b>&nbsp;&nbsp;</td> ";
			$list_page .= "<td><b style='color:#0000FF'>Trước</b>&nbsp;&nbsp;&nbsp;&nbsp;</td> "; 
		}
		
		for ( $i = $lowLimit; $i <= $highLimit; $i++ ) 
		{ 
			if($i == $page)	//trang đang chọn
				$list_page .= "<td><b class='active nav'>{$page} &nbsp;</b></td> "; 				
			else
				$list_page .= "<td><a href='#'><span onClick='GoTo($i)'>{$i}</span></a>&nbsp;</td>"; 				
		}
		
		if($page < $numPages)
		{
			$next = $page + 1;
			$list_page .= " <td>&nbsp;&nbsp;&nbsp;&nbsp;<a href='#'><b style='color:#0000FF' onClick='GoTo($next)'>Tiếp</b></a></td> "; 
			$list_page .= "<td>&nbsp;&nbsp;<a href='#'><b style='color:#0000FF' onClick='GoTo($numPages)'>Cuối</b></a></td> "; 
		}
		else
		{
			$list_page .= " <td>&nbsp;&nbsp;&nbsp;&nbsp;<b style='color:#0000FF'>Tiếp</b></td> "; 
			$list_page .= "<td>&nbsp;&nbsp;<b style='color:#0000FF'>Cuối</b></td>"; 		
		}		
	}  
		
	return $list_page;
}
?>